import java.util.HashMap;
import java.util.List;

public class CalcEquation {

    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
        int n = equations.size();
        HashMap<String, Integer> map = new HashMap<>(2 * n);
        Union union = new Union(2 * n);
        int id = 0;
        for (int i = 0; i < n; i++) {
            String x = equations.get(i).get(0);
            String y = equations.get(i).get(1);
            if (!map.containsKey(x)) {
                map.put(x, id++);
            }
            if (!map.containsKey(y)) {
                map.put(y, id++);
            }
            union.merge(map.get(x), map.get(y), values[i]);
        }
        double[] ans = new double[queries.size()];
        for (int i = 0; i < queries.size(); i++) {
            String x = queries.get(i).get(0);
            String y = queries.get(i).get(1);
            if (!map.containsKey(x) || !map.containsKey(y)) {
                ans[i] = -1.0;
            } else {
                ans[i] = union.cal(map.get(x), map.get(y));
            }
        }
        return ans;
    }

    private class Union {
        private int[] parent;
        private double[] weight;

        public Union(int n) {
            parent = new int[n];
            weight = new double[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
                weight[i] = 1.0;
            }
        }

        public int find(int i) {
            if (parent[i] != i) {
                int temp = parent[i];
                parent[i] = find(parent[i]);
                weight[i] *= weight[temp];
            }
            return parent[i];
        }

        public void merge(int i, int j, double value) {
            int ii = find(i);
            int jj = find(j);
            if (ii == jj) return;
            parent[ii] = jj;
            weight[ii] = weight[j] * value / weight[i];
        }

        public double cal(int i, int j) {
            int ii = find(i);
            int jj = find(j);
            if (ii == jj) {
                return weight[i] / weight[j];
            } else return -1;
        }
    }
}
